/**
 * 
 */
package com.fita.scheduler.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Update;
import com.demo2do.core.persistence.annotation.Variable;
import com.fita.scheduler.entity.Order;

/**
 * @author Guangli Yan
 *
 */
@Dao
public interface OrderDao {
	
	/**
	 * @param status
	 * @param maxPickupDate
	 * @param payType
	 * @param maxCreateTime
	 * @return
	 */
	@Query("SELECT id, code, alamo_code, pickup_date, return_date, status, brand, customer_name, create_time FROM orders WHERE is_disabled = 0 AND pickup_date < :maxPickupDate AND is_disabled = 0 AND status IN ( :status ) AND type = :payType AND create_time < :maxCreateTime ")
	List<Order> list(@Variable("status") List<String> status, @Variable("maxPickupDate") Date maxPickupDate, @Variable("payType") String payType, @Variable("maxCreateTime") Date maxCreateTime);

	/**
	 * @param order
	 */
	@Update("UPDATE orders SET status  = :status, cancel_time = :cancelTime WHERE id = :id ")
	void cancel(Order order);

	/**
	 * @param orderId 
	 * @return
	 */
	@Query("SELECT ci.surname as surname FROM client_info ci, order_details od, orders o WHERE o.details_id = od.id AND od.customer_info_id = ci.id AND o.id = :orderId ")
	Map<String, Object> getSurname(@Variable("orderId") Long orderId);


	/**
	 * @param payType
	 * @param orderStatus
	 * @param maxReturnDate
	 * @return
	 */
	@Query("SELECT id, code, alamo_code, pickup_date, return_date, status, brand, customer_name, create_time, distribution_reward_record_id FROM orders WHERE is_disabled = 0 AND type = :payType AND status = :orderStatus AND return_date < :maxReturnDate")
	List<Order> list(@Variable("payType") String payType, @Variable("orderStatus") String orderStatus, @Variable("maxReturnDate") Date maxReturnDate);

	/**
	 * @param order
	 */
	@Update("UPDATE orders SET status  = :status WHERE id = :id ")
	void consume(Order order);

	@Query("SELECT o.id, o.code, o.alamo_code, p.name as mark_pay_operator, o.mark_pay_time, o.actual_pay_price " +
			"FROM orders o INNER JOIN principal p ON o.mark_pay_operator_id = p.id " +
			"WHERE o.is_disabled = 0 AND o.mark_pay_time > :startMarkPayTime AND p.authority IN (:operatorTypes) ")
	List<Order> list(@Variable("startMarkPayTime") Date startMarkPayTime, @Variable("operatorTypes") List<String> operatorTypes);
}
